package com.lhy.tool.database.table;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties;
import com.baomidou.mybatisplus.extension.toolkit.JdbcUtils;
import com.lhy.tool.util.DbTypeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class DbMetaDataFactory {

    private static final String META_DATA_PROCESSOR = "MetaDataProcessor";
    @Autowired
    MybatisPlusProperties mybatisPlusProperties;

    @Autowired
    DataSourceProperties dataSourceProperties;

    @Autowired
    List<DbMetaDataProcessor> dbTableServices;

    public DbMetaDataProcessor dbMetaDataProcessor() {
        DbType dbType = JdbcUtils.getDbType(dataSourceProperties.getUrl());;
        return dbTableServices.stream()
                .filter(dbTableService -> DbTypeUtil.isMatch(dbTableService, META_DATA_PROCESSOR, dbType))
                .findFirst().orElseThrow(() -> new NullPointerException("根据当前dbType：" + dbType + "未找到对应 AbstractDbMetaDataProcessor"));
    }
}
